using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using CodeCampServerLite.Core.Domain;
using CodeCampServerLite.Core.Domain.Model;
using CodeCampServerLite.UI.Controllers;
using CodeCampServerLite.UI.Helpers;
using CodeCampServerLite.UI.Models;
using NHibernate.Linq;
using NUnit.Framework;
using Rhino.Mocks;
using Should.Extensions.AssertExtensions;

namespace CodeCampServerLite.UnitTests.UI
{
	[TestFixture]
	public class ConferenceControllerTests : TestBase
	{
		[Test]
		public void Should_map_the_conferences_to_the_viewmodel_for_index()
		{
			// Setup
			var conferences = new[]
			{
				new Conference("Conf 1"),
				new Conference("Conf 2")
			};
			var viewModel = new[]
			{
				new ConferenceListModel(),
				new ConferenceListModel(),
			};
			var repository = new DummyRepository(conferences);
			var mapper = Stub<IObjectMapper>();

			mapper.Stub(s => s.Map<IEnumerable<Conference>, ConferenceListModel[]>(conferences))
				.Return(viewModel);

			var controller = new ConferenceController(repository, mapper);

			// Execute
			var result = controller.Index();

			// Verify
			result.ViewData.Model.ShouldEqual(viewModel);
		}

		private class DummyRepository : IConferenceRepository
		{
			private readonly IEnumerable<Conference> _items;

			public DummyRepository(IEnumerable<Conference> items)
			{
				_items = items;
			}

			public Conference GetById(Guid id)
			{
				return _items.FirstOrDefault(item => item.Id == id);
			}

			public void Save(Conference entity)
			{
			}

			public IEnumerable<Conference> GetAll()
			{
				return _items;
			}

			public IQueryable<Conference> Query()
			{
				return _items.AsQueryable();
			}

			public void Delete(Conference entity)
			{
			}

			public Conference GetByName(string eventName)
			{
				return _items.FirstOrDefault(item => item.Name == eventName);
			}
		}
	}

}